<?php

namespace EM\NewsBundle\Controller;
use EM\NewsBundle\Entity\Geography;
use EM\NewsBundle\Form\GeographyType;
use EM\NewsBundle\Form\GeographyUpdateType;
use EM\NewsBundle\Form\GeoLangRelationType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class GeographyController extends Controller
{
    
    public function createAction(Request $request)
    {
	
                $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Geography');
                $geography = $repository->findAll();
		$geo = new Geography();
		$form = $this->createForm(new GeographyType(), $geo);
		$request = $this->getRequest();
		if($request->getMethod() == 'POST')
		{
			
			$form->bind($request);
                        $level=$this->get('request')->request->get('level');
                                $geoparent=$this->get('request')->request->get('geoparent');
				$geo->setGeoParent((int)$geoparent);
				$em = $this->getDoctrine()->getManager();
                                
                             
                                $em->persist($geo);
				$em->flush();
                                $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Geography');
                                $geography = $repository->findAll();
                                $geo=new Geography();
                                $form = $this->createForm(new GeographyType(), $geo);
				return $this->render('EMNewsBundle:Geography:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'geography' => $geography,
                                    'status_form'=>1
                                    )); 
			
		
		}
                
    return $this->render('EMNewsBundle:Geography:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'geography' => $geography,
                                    'status_form'=>0
                                    )); 
			            

    }

    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $repository=$em->getRepository('EMNewsBundle:Geography');
        $geography = $repository->findAll();	
        $geo=$repository->find($id);             
        $request = $this->getRequest();
        $name=$this->get('request')->request->get('name_geo');
        $geo->setName($name);
        $em->persist($geo);
	$em->flush();
          
            return $this->render('EMNewsBundle:Geography:_edit.html.twig', array(
                                    'id'=>$id,
                                    'geography' => $geography,
                                    'geo' => $geo,
                                    'status_form'=>5
                                    )); 
        
    }
    public function deleteAction($id)
    {
                
		$em = $this->getDoctrine()->getManager();
                $repository=$em->getRepository('EMNewsBundle:Geography');
                $geo=$repository->find($id);
                $childs=$repository->findByGeoParent($id); //Busca hijos de este item
                $geo = new Geography();
		$form = $this->createForm(new GeographyType(), $geo);
                $geography = $repository->findAll();
                if($childs){ // Si tiene hijos no eliminara el item
                    return $this->render('EMNewsBundle:Geography:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'geography' => $geography,
                                    'status_form'=>17
                                    )); 
                }
                else{//Elimina el item
                    $em->remove($geo);
                    $em->flush();
                    return $this->render('EMNewsBundle:Geography:_new.html.twig', array(
                                    'form' => $form->createView(),
                                    'id'=>$id,
                                    'geography' => $geography,
                                    'status_form'=>0
                                    )); 
                    
                }
                
                
    }

    public function showAction(){

            $repository=$this->getDoctrine()->getRepository('EMNewsBundle:Geography');
            $geography = $repository->findAll();	
            $geo = new Geography();
            $form = $this->createForm(new GeographyType(), $geo);
            return $this->render('EMNewsBundle:Geography:list.html.twig',array('geography' => $geography,  'form' => $form->createView(),'status_form'=>0));

    } 
    public function findAction($id){
         
        $em = $this->getDoctrine()->getManager();
        $repository=$em->getRepository('EMNewsBundle:Geography');
        $geo=$repository->find($id);
        $geography = $repository->findAll();	
        $form = $this->createForm(new GeographyUpdateType(), $geo);

        return $this->render('EMNewsBundle:Geography:_edit.html.twig', array(
				'form' => $form->createView(),
				'id' => $geo->getId(),
				'geo_parent' => $geo->getGeoParent(),
                                'geo' => $geo,
                                'geography' => $geography,
				'status_form'=>0
                            ));
        
    }
    
    public function findLevelsAction($level){
      
      $repository = $this->getDoctrine()->getRepository('EMNewsBundle:Geography');
        if($level==0){
            return $this->render('EMNewsBundle:Geography:_combo.html.twig', array('geography' => 0));  
      }else if($level==3){
        $geo = $repository->findByLevel(2);  
            return $this->render('EMNewsBundle:Geography:_combo2.html.twig', array('geography' => $geo));  
      } 
      
      else{
        $geo = $repository->findByLevel($level);

        if($geo!=null){
           // print_r($geo);
            return $this->render('EMNewsBundle:Geography:_combo.html.twig', array('geography' => $geo));  
        }
        else
        {
            return $this->render('EMNewsBundle:Geography:_combo.html.twig', array('geography' => null));  
          
        }
        }
    }
    public function findParentAction($parent){
      
      $repository = $this->getDoctrine()->getRepository('EMNewsBundle:Geography');
        if($parent==0){
            return $this->render('EMNewsBundle:Geography:_combo.html.twig', array('geography' => 0));  
        } 
        else{
        $geo = $repository->findByGeoParent($parent);

        if($geo!=null){
           // print_r($geo);
            return $this->render('EMNewsBundle:Geography:_combo.html.twig', array('geography' => $geo));  
        }
        else
        {
            return $this->render('EMNewsBundle:Geography:_combo.html.twig', array('geography' => null));  
          
        }
        }
    }
    public function langSelectionAction(){
        $em = $this->getDoctrine()->getManager();
        $geolang = new \EM\NewsBundle\Entity\GeoLangRelation();
        $form = $this->createForm(new GeoLangRelationType(), $geolang);
        $languages=$em->getRepository('EMNewsBundle:Languages')->findAll();
        $geolanglist=$em->getRepository('EMNewsBundle:Geography')->findByLevel(2);
       // $geolanglist= $repository2->findByLevel(2);
        return $this->render('EMNewsBundle:Geography:LanguageSelection.html.twig'
                                , array(
                                    'form' => $form->createView(),
                                    'languages' => $languages,
                                    'geolanglist' => $geolanglist
                )); 
    }
    public function saveSelectionAction(Request $request){
        $em = $this->getDoctrine()->getManager();
        $languagesl = $this->get('request')->request->get('lang');
        $languages=$em->getRepository('EMNewsBundle:Languages')->findAll();
        $geolanglist=$em->getRepository('EMNewsBundle:Geography')->findByLevel(2);
        $updated=0;
        if($languages!="")
        {
            foreach($languagesl as $ln){
              //  echo "Lang: ".$ln;
               
              //  return $ln;
              //  exit();
            $geolang = new \EM\NewsBundle\Entity\GeoLangRelation();
            $form = $this->createForm(new GeoLangRelationType(), $geolang);
            $request = $this->getRequest();
            $form->bind($request);
            $lang=$em->getRepository('EMNewsBundle:Languages')->find($ln);
            $exists=$em->getRepository('EMNewsBundle:GeoLangRelation')->findBy(array('geo'=>$geolang->getGeo(),'lang'=>$lang->getId()));
          //  echo count($exists);
             $updated=0;
            if(count($exists)==0){
            $geolang->setLang($lang);

            $em->persist($geolang);
            if(!$em->flush()){
                $updated=1;
            }
            else
            {
               return $this->render('EMNewsBundle:Geography:langGeoTable.html.twig'
                                , array(
                                    'geolanglist' => $geolanglist,
                                    'updated' => $updated
                )); 
            }
            }
            else
            {
                $updated=2;
                
            }
        }
     }
          return $this->render('EMNewsBundle:Geography:langGeoTable.html.twig'
                                , array(
                                    'geolanglist' => $geolanglist,
                                    'updated' => $updated
                )); 
         
    }
    
    public function editRelationAction(){
        
        
    }
}
